﻿#region License

// // -------------------------------------------------------------------------------------
// // This information refers to InstrumentationWrapperService.cs
// // 
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file (InstrumentationWrapperService.cs) is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using DishDash.Common;
using MessageAPI;
using MessageAPI.Core.Implementation;

#endregion

namespace InstrumentationWrapper
{
    public class InstrumentationWrapperService
    {
        private static IPerformanceInstrumentationService _performanceInstrumentationService;
        private static InstrumentationWrapperService _instance;
        private static readonly object Lockobject = new object();

        private InstrumentationWrapperService()
        {
        }

        public static InstrumentationWrapperService Instance
        {
            get
            {
                lock (Lockobject)
                {
                    if (_instance == null)
                    {
                        _instance = new InstrumentationWrapperService();
                    }
                    return _instance;
                }
            }
        }

        public static void LogDuration(string eventToLog, DateTime startTime)
        {
            if (_performanceInstrumentationService == null)
            {
                IPerformanceInstrumentationServiceFactory performanceInstrumentationServiceFactory =
                    new PerformanceInstrumentationServiceFactory();
                _performanceInstrumentationService =
                    performanceInstrumentationServiceFactory.GetPerformanceInstrumentationService();
            }
            //Calculate method execution duration
            var duration = DateTime.Now - startTime;

            //Calculate time referenced to Unix epoch
            var startOfEpoch = new DateTime(1970, 1, 1);
            long ticksSinceEpoch = DateTime.Now.Ticks - startOfEpoch.Ticks;

            var performanceEvent = new PerformanceEvent
                                       {
                                           Category = eventToLog,
                                           TimeStamp = ticksSinceEpoch,
                                           Value = duration.Ticks
                                       };
            _performanceInstrumentationService.SendEvent(performanceEvent);
        }

        public static void LogCount(string eventToLog)
        {
            if (_performanceInstrumentationService == null)
            {
                IPerformanceInstrumentationServiceFactory performanceInstrumentationServiceFactory =
                    new PerformanceInstrumentationServiceFactory();
                _performanceInstrumentationService =
                    performanceInstrumentationServiceFactory.GetPerformanceInstrumentationService();
            }

            long ticksSinceEpoch = DateTime.Now.Ticks;

            var performanceEvent = new PerformanceEvent
                                       {
                                           Category = eventToLog,
                                           TimeStamp = ticksSinceEpoch,
                                           Value = 1
                                       };
            _performanceInstrumentationService.SendEvent(performanceEvent);
        }
    }
}